package com.lg.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.template.mapper.plus.PlusUser;
import com.lg.template.entity.User;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Api(tags = "Plus测试")
@RestController
@RequestMapping("/plus")
public class PlusController {
    @Autowired
    private PlusUser userMapper;

    @Value("${server.port}")
    private String serverPort;



    @GetMapping("selectByAnnotation")
    public List<User> selectByAnnotation(){
        List<User> users = userMapper.selectByAnnotation();
        users.forEach(System.out::println);
        return users;
    }

    @GetMapping("selectOneByAnnotation")
    public User selectOneByAnnotation(){
        User user = userMapper.selectOneByAnnotation();
        System.out.println(user);
        return user;
    }

    @GetMapping("insertByAnnotation")
    public Integer insertByAnnotation(){
        User user = new User();
        user.setName("annotationInsertName");
        user.setId(77);
        Integer res = userMapper.insertByAnnotation(user);
        System.out.println(user);
        return res;
    }

    @PostMapping("updateByAnnotation")
    public Integer updateByAnnotation(@RequestBody User user){

        Integer res = userMapper.updateByAnnotation(user);
        System.out.println(user);
        return res;
    }

    @GetMapping("deleteByAnnotation")
    public Integer deleteByAnnotation(Integer id){
        Integer res = userMapper.deleteByAnnotation(id);
        return res;
    }





    @GetMapping("queryById")
    public User queryById(){
        User user = userMapper.selectById(8);
        System.out.println(user);
        return user;
    }

    @GetMapping("queryByIds")
    public   List<User>  queryByIds(){
        List<User> users = userMapper.selectBatchIds(Arrays.asList(7, 8, 9));
        users.forEach(System.out::println);
        return users;
    }

    @GetMapping("queryByMap")
    public   List<User>  queryByMap(){
        HashMap<String, Object> parameter = new HashMap<>();
        parameter.put("name", "randomName");
        parameter.put("pwd", "root");
        List<User> users = userMapper.selectByMap(parameter);
        users.forEach(System.out::println);
        return users;
    }

    @GetMapping("queryByPage")
    public   List<User>  queryByPage(){
        Page<User> userPage = new Page<>(1,2);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                .likeRight("name", "ran")
                .orderByAsc("id");

        Page<User> page = userMapper.selectPage(userPage, wrapper);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @DeleteMapping("deleteById")
    public   Integer deleteById(Integer num){
        return userMapper.deleteById(num);
    }

    @DeleteMapping("deleteByIds")
    public   Integer deleteByIds(){
        return userMapper.deleteBatchIds(Arrays.asList(1,2,7));
    }

    @DeleteMapping("deleteByMap")
    public   Integer deleteByMap(){
        HashMap<String, Object> parameter = new HashMap<>();
        parameter.put("name", 1);
        return userMapper.deleteByMap(parameter);
    }



    @PostMapping("queryAllUser")
    public   List<User>  queryAllUser(){
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
        return users;
    }

    @PostMapping("insertUser")
    public   Integer  insertUser(@RequestBody User user){
        int res = userMapper.insert(user);
        System.out.println(user);
        return res;
    }

    @PostMapping("updateUser")
    public   Integer  updateUser(@RequestBody User user){
        User user1 = userMapper.selectById(user.getId());
        user1.setName("乐观锁测试");
        int res = userMapper.updateById(user1);
        System.out.println(user1);
        return res;
    }











    @GetMapping("/baseConfig")
    public String getBaseConfig(){
        return serverPort;
    }


}
